import 'package:flutter/material.dart';
import 'package:xview/routers/index.dart';
import 'package:xview/utils/dimension.dart';

class XNavbar{
  Color color;
  String title;
  Color titleColor;
  num titleSize;
  FontWeight titleFontWeight;
  IconData leftIcon;
  Color leftIconColor;
  double leftIconSize;
  Function? customBack; //自定义返回逻辑
  bool centerTitle;
  double elevation; //阴影大小
  bool isBack; //是否展示返回框
  List<Widget>? actions;
  Widget? leading;
  Widget? content;
  double contentHeight;

  XNavbar({
    this.color = Colors.white,
    this.title = '标题',
    this.titleColor = const Color(0xFF1F2329),
    this.titleSize = 17,
    this.titleFontWeight = FontWeight.bold,
    this.isBack = true,
    // this.leftIcon = const IconData(0xe749,fontFamily: 'appIcon'),
    this.leftIcon = Icons.arrow_back_ios_rounded,
    this.leftIconColor = const Color(0xff606266),
    this.leftIconSize = 24,
    this.centerTitle = true,
    this.elevation = 0, //下划线
    this.customBack,
    this.actions,
    this.leading, // 自定义左侧leading
    this.content, // 自定义内容
    this.contentHeight = 30, // 自定义内容高度
  });

  render(context){
    return AppBar(
      title: renderTitle(context),
      leading: renderLeading(context),
      actions: actions,
      centerTitle: centerTitle,
      backgroundColor: color,
      elevation: elevation,
      titleSpacing: 0.0,
      automaticallyImplyLeading: false,
    );
  }

  renderTitle(context){
    if(content == null){
      return Text("${title}",style: TextStyle(color: titleColor,fontSize: Dimension(context).toPx(titleSize),fontWeight: titleFontWeight));
    }else{
      return Container(
        height: contentHeight,
        child: Stack(
          clipBehavior: Clip.none,
          children: [
            Center(
              child: Text("${title}",style: TextStyle(color: titleColor,fontSize: Dimension(context).toPx(titleSize),fontWeight: titleFontWeight)),
            ),
            Positioned(
              left: 0,
              top: 0,
              height: contentHeight,
              child: content ?? Container(),
            )
          ],
        )
      );
    }
  }

  renderLeading(context){
    if(leading == null && content == null){
      return Offstage(
        offstage: !isBack || content != null,
        child:GestureDetector(
          onTap: (){
            customBack ?? MyRouter.back(context);
          },
          child: Icon(leftIcon,color: leftIconColor,size: leftIconSize),
        ),
      );
    }

    if(leading != null){
      return leading;
    }

    if(leading == null && content != null){
      return null;
    }
  }

}


